<?php
namespace app\index\controller;
use catetree\Catetree;
class Category extends Base
{   
    public function index($id)
    {

        $TpCategory=db('tp_category');
        $topId=input('topId');
        $Sid=input('Sid');
        //导航栏信息
        $topIdRes=$TpCategory->where('id',$topId)->find();//顶级栏目
        $secIdRes=$TpCategory->where('id',$Sid)->find();//二级栏目
        $sonIdRes=model('TpCategory')->getSoncates($topId);//顶级栏目下的所有子栏目

        
        $categoryRes=$TpCategory->find($id);
        //栏目筛选属性
        $sai=$categoryRes['search_attr_ids'];
        $searchAttrs=db('tp_attr')->where(array('id'=>array('in',$sai)))->select();


        
        $Catetree=new Catetree();
        $cateIds=array();
        $cateIds=$Catetree->childrenids($id,db('tp_category'));
        $cateIds[]=intval($id);

        //栏目价格区间
        $goodsPrice=db('tp_goods')->field('min(shop_price) min_price ,max(shop_price) max_price')->where(['category_id'=>['in',$cateIds]])->find();
        $maxPrice=intval($goodsPrice['max_price']);
        $minPrice=intval($goodsPrice['min_price']);
        $priceSection=($maxPrice-$minPrice)/$categoryRes['pr_num'];
        // dump($priceSection);die;
        $priceRange=array();
        if($minPrice){
            for($i=0;$i<$categoryRes['pr_num'];$i++){
                if($i==0){
                    $priceRange[]=($minPrice).'-'.round(($minPrice+$priceSection));
                    $minPrice=round(($minPrice+$priceSection));
                }elseif($i==($categoryRes['pr_num']-1)){
                    $priceRange[]=($minPrice+1).'-'.$maxPrice;
                }else{
                    $priceRange[]=($minPrice+1).'-'.round(($minPrice+$priceSection));
                    $minPrice=round(($minPrice+$priceSection));
                    }
                }
            }

     
        //获取商品品牌的信息
        $goodsRes=db('tp_goods')->where(['category_id'=>['in',$cateIds]])->select();
        $brandIds=array();
        foreach ($goodsRes as $k => $v) {
            $brandIds[]=$v['brand_id'];
        }
        $brandIds=array_unique($brandIds);//去掉重复值
        $brandIds=implode(',', $brandIds);
        $brandRes=db('tp_brand')->where(array('id'=>array('in',$brandIds)))->select();


        $goodsInfo=model('TpGoods')->goodsInfo($id);
        // dump($goodsInfo);die;





       
        $this->assign([
            'goodsInfo'=>$goodsInfo,
            'topIdRes'=>$topIdRes,
            'secIdRes'=>$secIdRes,
            'sonIdRes'=>$sonIdRes,
            'brandRes'=>$brandRes,
            'priceRange'=>$priceRange,
            'searchAttrs'=>$searchAttrs,

        ]);


        return view('Category');
    }



    public function getCateInfo($id){
        //处理商品分类
        $CatesRes=model('TpCategory')->getSoncates($id);
        $data=array();
        $cat='';
        foreach ($CatesRes as $k => $v) {
        $cat.='<dl class="dl_fore1"><dt><a href="'.url('index/Category/index',array('id'=>$v['id'],'topId'=>$id,'Sid'=>$v['id'])).'" target="_blank">'.$v['cate_name'].'</a></dt><dd>';
            foreach ($v['children'] as $k1 => $v1) {
                $cat.='<a href="'.url('index/Category/index',array('id'=>$v1['id'],'topId'=>$id,'Sid'=>$v['id'])).'" target="_blank">'.$v1['cate_name'].'</a>';
            }
            $cat.='</dd></dl>'; 
        }

        //处理推荐词关联
        $CategoryWords=model('TpCategory')->getCatesWords($id);
        // dump($CategoryWords);die;
        $channels='';
        foreach ($CategoryWords as $k => $v) {
            $channels.='<a href="'.$v['link_url'].'" target="_blank">'.$v['words'].'</a>';
        }
        
        $data=model('TpCategory')->getCatesBrands($id);
        $brands='';
        $brands.='<div class="cate-brand">';
        foreach ($data['brands_id'] as $k => $v) {
            $brands.='<div class="img">
                        <a href="'.$v["brand_url"].'" target="_blank"><img src="/public/static/uploads/'.$v["brand_img"].'"></a>
                    </div>';
        }
        
        $brands.='</div>';
        $brands.='<div class="cate-promotion"><a href="'.$data["pro_url"].'" target="_blank"><img src="/public/static/uploads/'.$data["pro_img"].'" width="200"></a></div>';
        $data['topic_content']=$channels;
        $data['cat_content']=$cat;
        $data['brands_ad_content']=$brands;
        $data['cat_id']=$id;
        return json($data);
    }
}
